Skip to content

fix: preserve guardrail_latest_message wrapping after tool execution#1658

Merged
lizradway merged 3 commits intostrands-agents:mainfrom
austinmw:fix/guardrail-latest-message-tool-result
Feb 27, 2026
Merged

fix: preserve guardrail_latest_message wrapping after tool execution#1658
lizradway merged 3 commits intostrands-agents:mainfrom
austinmw:fix/guardrail-latest-message-tool-result

Conversation

@austinmw
Copy link
Contributor

Summary

  • Fixes guardrail_latest_message wrapping being lost after tool execution, when the last message is a toolResult with no text/image content
  • Pre-computes the index of the last user message containing text or image content before the formatting loop, so guardContent wrapping targets the correct message regardless of what follows it

Fixes #1651

Test plan

  • New test test_format_request_with_guardrail_latest_message_after_tool_use — verifies guardContent wraps the user text message when a toolResult follows
  • New test test_format_request_with_guardrail_latest_message_wraps_final_user_text — verifies existing behavior (last message is text) still works
  • Existing test test_format_request_with_guardrail_latest_message passes (no regression)
  • Full bedrock test suite passes (104 passed, 3 skipped)

🤖 Generated with Claude Code

Unshure
Unshure previously approved these changes Feb 26, 2026
lizradway
lizradway previously approved these changes Feb 26, 2026
@Unshure Unshure marked this pull request as draft February 27, 2026 19:01
@Unshure Unshure marked this pull request as ready for review February 27, 2026 19:01
@codecov
Copy link

codecov bot commented Feb 27, 2026

Codecov Report

✅ All modified and coverable lines are covered by tests.

📢 Thoughts on this report? Let us know!

@lizradway lizradway dismissed stale reviews from Unshure and themself via d331c91 February 27, 2026 19:48
@github-actions github-actions bot added size/xl and removed size/m labels Feb 27, 2026
@lizradway lizradway force-pushed the fix/guardrail-latest-message-tool-result branch from d331c91 to 9493084 Compare February 27, 2026 19:50
@github-actions github-actions bot added size/m and removed size/xl labels Feb 27, 2026
@github-actions github-actions bot added size/m and removed size/m labels Feb 27, 2026
@lizradway lizradway requested a review from Unshure February 27, 2026 20:04
@lizradway lizradway self-requested a review February 27, 2026 20:04
austinmw and others added 3 commits February 27, 2026 15:14
When guardrail_latest_message=True, the guardContent wrapping was lost
after tool execution because the last message was a toolResult (with no
text/image content). Pre-compute the index of the last user message
containing text or image content so wrapping is applied correctly even
when subsequent toolResult messages follow.

Fixes strands-agents#1651

Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com>
@lizradway lizradway force-pushed the fix/guardrail-latest-message-tool-result branch from bde90ea to 1a68f8d Compare February 27, 2026 20:14
@github-actions github-actions bot added size/m and removed size/m labels Feb 27, 2026
@lizradway lizradway deployed to manual-approval February 27, 2026 20:14 — with GitHub Actions Active
@github-actions
Copy link

Assessment: Approve

Clean bug fix that correctly addresses the issue where guardrail_latest_message wrapping was lost after tool execution. The implementation pre-computes the index of the last user text/image message, ensuring guardContent wrapping targets the correct message regardless of subsequent toolResult messages.

Review Summary
  • Implementation: Clean and follows existing patterns. New private helper method _find_last_user_text_message_index() is well-designed with proper type hints and docstrings.
  • Testing: Comprehensive coverage with 6 new unit tests for the helper method and 5 integration tests covering various scenarios (multiple tool calls, images before tool results, parallel tool results).
  • All tests pass locally.

Nice fix for a subtle but impactful bug! 🎉

@lizradway lizradway merged commit c50457d into strands-agents:main Feb 27, 2026
22 of 24 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

Projects

None yet

Development

Successfully merging this pull request may close these issues.

[BUG] guardrail_latest_message wrapping lost after tool execution (toolResult becomes last message)

3 participants